package com.smartgame.screens;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.smartgame.controllers.AbstractController;
import com.smartgame.renderers.AbstractRenderer;

public abstract class AbstractScreen implements Screen {

	protected Game game;
	protected AbstractController controller;
	protected AbstractRenderer renderer;
	
	private boolean paused;

	public AbstractScreen(Game game) {
		this.game = game;
		// enable input for next screen
		Gdx.input.setInputProcessor(this.getInputProcessor());
	}

	public AbstractScreen() {
	}

	@Override
	public void render(float deltaTime) {
		// Do not update game world when paused.
		if (!paused) {
			// Update game world by the time that has passed
			// since last rendered frame.
			deltaTime = Math.min(Gdx.graphics.getDeltaTime(), 1 / 60f);
			controller.update(deltaTime);
		}
		// Sets the clear screen color to: Cornflower Blue
		Gdx.gl.glClearColor(0x64 / 255.0f, 0x95 / 255.0f, 0xed / 255.0f,
				0xff / 255.0f);
		// Clears the screen
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		// Render game world to screen
		renderer.render();
	}

	@Override
	public void resize(int width, int height) {
		renderer.resize(width, height);
	}

	@Override
	public void show() {
		controller = createAbstractController();
		renderer = createAbstractRenderer();
		Gdx.input.setCatchBackKey(true);
	}

	@Override
	public void hide() {
		renderer.dispose();
		Gdx.input.setCatchBackKey(false);
	}

	/**
	 * The app might be killed after this method is called.<br>
	 * we should know this and make save/clear job here!
	 * 
	 * @see com.badlogic.gdx.Screen#pause()
	 */
	@Override
	public void pause() {
		paused = true;
		saveScreenStatus();
	}

	@Override
	public void resume() {
		// Need it?
//		Assets.instance.init(new AssetManager()); 
		// Only called on Android!
		paused = false;
	}
	
	@Override
	public void dispose() {
		// do nothing here!
	}
	
	/**
	 * Override me if you want to save screen status.
	 */
	protected void saveScreenStatus() {

	}

	protected abstract InputProcessor getInputProcessor();
	
	protected abstract AbstractController createAbstractController();
	
	protected abstract AbstractRenderer createAbstractRenderer();

}
